﻿using System;
using System.Collections.Generic;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using System.Data;
using ZX.CRM.Report.CrmReport;
using System.Web.UI;
namespace ZX.WebApp.Crm.Report
{
    public partial class RptCrmValueInfo : BasePage
    {
        BCrmReport BReport = new BCrmReport();
        public string fcid = "";
        string sType;
        public string SType
        {
            get { return sType; }
            set { sType = value; }
        }
        string sId;

        public string SId
        {
            get { return sId; }
            set { sId = value; }
        }
        string sDept;

        public string SDept
        {
            get { return sDept; }
            set { sDept = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            hdnId.Value = this.UserId.ToString();
            fcid = Request.QueryString["functionID"] == null ? "" : Request.QueryString["functionID"];
            sType = Request.QueryString["type"] == null ? "" : Request.QueryString["type"];
            sDept = Request.QueryString["id"] == null ? "" : Request.QueryString["id"];
            sId = Request.QueryString["dept"] == null ? "" : Request.QueryString["dept"];
            if (!IsPostBack)
            {
                string strStart = (Request.QueryString["stime"] == null || Request.QueryString["stime"] == "") ? DateTime.Now.Year.ToString() + "-01-01" : Request.QueryString["stime"];
                txtStime.Value = strStart;
                txtEtime.Value = (Request.QueryString["etime"] == null || Request.QueryString["etime"] == "") ? DateTime.Now.Year.ToString() + "-12-31" : Request.QueryString["etime"];
            }
            bind(sDept, sType);
        }
        protected void btnDone_Click(object sender, EventArgs e)
        {
            getStartEndDate();
            fcid = Request.QueryString["functionID"] == null ? "" : Request.QueryString["functionID"];
            sType = Request.QueryString["type"] == null ? "" : Request.QueryString["type"];
            sDept = Request.QueryString["id"] == null ? "" : Request.QueryString["id"];
            sId = Request.QueryString["dept"] == null ? "" : Request.QueryString["dept"];
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "msg", "<script>parent.GetSetStartTime('" + txtStime.Value + "');parent.GetSetEndTime('" + txtEtime.Value + "'); </script>");
            bind(sDept, sType);
        }
        private void getStartEndDate()
        {
            if (hdnFd.Value == "month")
            {
                txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
            }
            else if (hdnFd.Value == "year")
            {
                txtStime.Value = DateTime.Now.Year.ToString() + "-01-01";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
            }
            else if (hdnFd.Value == "season")
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 1)
                {
                    txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 2)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
            }
        }
        private void getCss()
        {
            if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
            }
            else if (txtStime.Value == DateTime.Now.Year.ToString() + "-01-01" &&
                txtEtime.Value == DateTime.Now.Year.ToString() + "-12-31")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "red");
            }
            else
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 1)
                {
                    if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 2)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
            }
        }
        private void bind(string sdept, string stype)
        {
            DoReport(sdept, DateTime.Parse(txtStime.Value), DateTime.Parse(txtEtime.Value), stype);
            getCss();
        }
        /// <summary>
        /// 生成报表
        /// </summary>
        /// <param name="Idept">部门ID</param>
        /// <param name="startdate">开始时间</param>
        /// <param name="enddate">结束时间</param>
        /// <param name="type">0:营销中心1:分公司2:团队3:个人</param>
        private void DoReport(string Idept, DateTime startdate, DateTime enddate, string type)
        {
            if (Idept == "" || type == "")
            {
                Idept = this.UserId.ToString();
                type = "3";
            }

            DataSet myDataSet = new DataSet();
            myDataSet.DataSetName = "DsCrmValue";
            DataTable AllbackMoney = new DataTable();
            DataTable TermBackMoney = new DataTable();
            DataTable PersonBackMoney = new DataTable();
            string strAllDeptId = "FB282647-FFE0-440B-81B2-1466D3EAC417";
            if (type == "0")  //高层
            {
                //获取所有分公司的回款信息
                AllbackMoney = BReport.ReturnTableAllValueDept(Idept, type, startdate, enddate);
                //获取团队前20名
                TermBackMoney = BReport.ReturnTableAllTeamValue(strAllDeptId, type, startdate, enddate);
                //获取个人前20名
                PersonBackMoney = BReport.ReturnTableAllPersonValue(strAllDeptId, type, startdate, enddate);
            }
            else if (type == "1") //分公司总经理
            {
                //获取所有分公司的回款信息
                AllbackMoney = BReport.ReturnTableAllValueDept(Idept, type, startdate, enddate);
                //获取团队前20名
                TermBackMoney = BReport.ReturnTableAllTeamValue(strAllDeptId, type, startdate, enddate);
                //获取个人前20名
                PersonBackMoney = BReport.ReturnTableAllPersonValue(strAllDeptId, type, startdate, enddate);
            }
            else if (type == "2")  //团队负责人
            {
                //获取所有分公司的回款信息
                AllbackMoney = BReport.ReturnTableAllValueDept(Idept, type, startdate, enddate);
                //获取团队前20名
                TermBackMoney = BReport.ReturnTableAllTeamValue(strAllDeptId, type, startdate, enddate);
                //获取个人前20名
                PersonBackMoney = BReport.ReturnTableAllPersonValue(strAllDeptId, type, startdate, enddate);

            }
            else if (type == "3") //个人
            {
                //获取所有分公司的回款信息
                AllbackMoney = BReport.ReturnTableAllValueDept(Idept, type, startdate, enddate);
                //获取团队前20名
                TermBackMoney = BReport.ReturnTableAllTeamValue(strAllDeptId, type, startdate, enddate);
                //获取个人前20名
                PersonBackMoney = BReport.ReturnTableAllPersonValue(strAllDeptId, type, startdate, enddate);
            }
            decimal totalMoney = 0;
            if (AllbackMoney!=null&&AllbackMoney.Rows.Count>0)
            {
                for (int i = 0; i < AllbackMoney.Rows.Count; i++)
                {
                    totalMoney = totalMoney + (decimal)AllbackMoney.Rows[i][1];
                }
            }

            lblTotalMoney.Text = totalMoney.ToString();
            AllbackMoney.TableName = "dtDept";
            TermBackMoney.TableName = "dtZj";
            PersonBackMoney.TableName = "dtJl";
            myDataSet.Tables.Add(AllbackMoney.Copy());
            myDataSet.Tables.Add(TermBackMoney.Copy());
            myDataSet.Tables.Add(PersonBackMoney.Copy());
            RptCrmValue crm = new ZX.CRM.Report.CrmReport.RptCrmValue();
            crm.DataSource = myDataSet;
            RV.Report = crm;
        }
    }
}